<?php

namespace app\admin\model;

use think\Model;
use traits\model\SoftDelete;

class Equipmentlist extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'equipment_list';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'equipment_status_text',
        'project_id_text',
        'contract_id_text',
    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    public function getEquipmentStatusList()
    {
        return ['0' => __('Equipment_status 0'), '1' => __('Equipment_status 1')];
    }

    public function getEquipmentStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['equipment_status']) ? $data['equipment_status'] : '');
        $list = $this->getEquipmentStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getProjectIdTextAttr($value, $data) {
        $str = Project::where(['id'=>$data['project_id']])->value('title');
        return $str ? $str : '-';
    }

    public function getContractIdTextAttr($value, $data) {
        $str = Contract::where(['id'=>$data['contract_id']])->value('contract_number');
        return $str ? $str : '-';
    }


}
